package com.xunyin.modules.mtdianping.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xunyin.api.ApiResult;
import com.xunyin.common.bean.LocalUser;
import com.xunyin.common.interceptor.AuthCheck;
import com.xunyin.dianping.DianpingCouponService;
import com.xunyin.dianping.DianpingPrepareResult;
import com.xunyin.dianping.DianpingResult;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.mtstore.domain.MtStore;
import com.xunyin.modules.mtstore.service.StoreService;
import com.xunyin.modules.mtstore.service.mapper.MtStoreMapper;
import com.xunyin.modules.user.domain.YxUser;
import com.xunyin.modules.user.vo.YxUserQueryVo;
import com.xunyin.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 美团点评团购套餐类controller
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Slf4j
@RestController
@Api(value = "共享空间:美团点评团购套餐", tags = "共享空间:美团点评团购套餐")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClientDianpingController {

    private final StoreService storeService;

    private final MtStoreMapper mtStoreMapper;

    private final DianpingCouponService dianpingCouponService;


    // https://e.dianping.com/dz-open/merchant/auth?app_key=f9e039e3da9ea9ec&redirect_url=https://cn.bing.com&state=teststate

    // https://e.dianping.com/dz-open/merchant/auth?app_key=f9e039e3da9ea9ec&redirect_url=https://shareminiapi2.tonetn.com/api/dianping/callBackUrl&state=teststate

    @GetMapping("/dianping/callBackUrl")
    @ApiOperation(value = "美团点评团购套餐", notes = "美团点评团购套餐", response = YxUserQueryVo.class)
    @CrossOrigin
    public void callBackUrl(HttpServletRequest request, @RequestParam("state") String state, @RequestParam("auth_code") String auth_code) {
        // 根据门店id,auth_code存储到对应的门店表中
        log.info("callBackUrl state:" + state + ",auth_code:" + auth_code);
        //MtStore mtStore = storeService.queryStoreById(storeId);
        Integer storeId = Integer.parseInt(state);
        String authCode = auth_code;
        MtStore mtStore = new MtStore();
        mtStore.setId(storeId);
        mtStore.setAuthCode(authCode);
        LambdaQueryWrapper<MtStore> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(MtStore::getId, storeId);
        mtStoreMapper.update(mtStore, wrapper1);
        dianpingCouponService.getToken(storeId);

    }

    /**
     * 美团输码验券校验接口
     *
     * @return ApiResult<DianpingPrepareResult>
     */
    @AppLog(value = "美团输码验券校验接口", type = 1)
    @AuthCheck
    @PostMapping("/prepare")
    @ApiOperation(value = "美团输码验券校验接口", notes = "美团输码验券校验接口")
    public ApiResult<DianpingPrepareResult> prepare(HttpServletRequest request, @RequestBody Map<String, Object> param) {
        //Integer storeId,String receiptCode
        Integer storeId = (param.get("storeId") == null) ? 0 : Integer.parseInt(param.get("storeId").toString());
        String receiptCode = (param.get("receiptCode") == null) ? "0" : param.get("receiptCode").toString();
/*        MtStore mtStore = storeService.queryStoreById(storeId);
        if(mtStore!=null){

        }*/
        DianpingPrepareResult dianpingPrepareResult = dianpingCouponService.prepare(storeId, receiptCode);
        dianpingPrepareResult.setRandom(RandomUtil.getRandom(5));
        return ApiResult.ok(dianpingPrepareResult);
    }


    /**
     * 扫码验券校验
     *
     * @return ApiResult<DianpingPrepareResult>
     */
    @AppLog(value = "美团扫码验券校验接口", type = 1)
    @AuthCheck
    @PostMapping("/scanPrepare")
    @ApiOperation(value = "美团扫码验券校验接口", notes = "美团扫码验券校验接口")
    public ApiResult<DianpingPrepareResult> scanPrepare(HttpServletRequest request, @RequestBody Map<String, Object> param) {
        Integer storeId = (param.get("storeId") == null) ? 0 : Integer.parseInt(param.get("storeId").toString());
        String qrCode = (param.get("qrCode") == null) ? "0" : param.get("qrCode").toString();
        DianpingPrepareResult dianpingPrepareResult = dianpingCouponService.scanPrepare(storeId, qrCode);
        return ApiResult.ok(dianpingPrepareResult);
    }


    //
    // @PostMapping("/consume")

    /**
     * 美团验券接口
     *
     * @return ApiResult<DianpingPrepareResult>
     */
    @AppLog(value = "美团验券接口", type = 1)
    @AuthCheck
    @PostMapping("/consume")
    @ApiOperation(value = "美团验券接口", notes = "美团验券接口")
    public ApiResult<DianpingResult> consume(HttpServletRequest request, @RequestBody Map<String, Object> param) {
        Integer storeId = (param.get("storeId") == null) ? 0 : Integer.parseInt(param.get("storeId").toString());
        String receiptCode = (param.get("receiptCode") == null) ? "0" : param.get("receiptCode").toString();
        Integer count = (param.get("count") == null) ? 0 : Integer.parseInt(param.get("count").toString());
        //Integer userId = (param.get("userId") == null) ? 0 : Integer.parseInt(param.get("userId").toString());
        YxUser yxUser = LocalUser.getUser();
        Integer userId = yxUser.getUid().intValue();
        String random = (param.get("random") == null) ? "0" : param.get("random").toString();

        DianpingResult dianpingResult = dianpingCouponService.consume(userId, storeId, receiptCode, count, random);
        return ApiResult.ok(dianpingResult);
    }


    /**
     * 同步团购信息与在后台选择团购套餐Id对应卡券关联
     * @return ApiResult<DianpingPrepareResult>
     */
/*    @AppLog(value = "美团验券接口", type = 1)
    @AuthCheck
    @PostMapping("/syncGroupInfor")
    @ApiOperation(value = "美团验券接口",notes = "美团验券接口")
    public ApiResult<String> syncGroupInfor(HttpServletRequest request, @RequestBody Map<String, Object> param){

    }*/
}
